package vn.edu.saigontech.ncms.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UnicodeUtil {
	
	private static Map<Character, List<Character>> characterMap = new HashMap<Character, List<Character>>();
	
	static {
		characterMap.put('a', Arrays.asList('â', 'ă', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'ạ', 'á', 'à', 'ả', 'ã'));
		characterMap.put('e', Arrays.asList('ê', 'ế', 'ề', 'ể', 'ễ', 'ệ', 'é', 'è', 'ẽ', 'ẹ', 'ẻ'));
		characterMap.put('o', Arrays.asList('ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ọ', 'ó', 'ò', 'õ', 'ỏ'));
		characterMap.put('u', Arrays.asList('ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự', 'ú', 'ù', 'ủ', 'ũ', 'ụ'));
		characterMap.put('i', Arrays.asList('í', 'ì', 'ỉ', 'ĩ', 'ị'));
		characterMap.put('d', Arrays.asList('đ'));
		characterMap.put('y', Arrays.asList('ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ'));
	}
	
	public static String removeVietnamese(String text) {
		StringBuilder builder = new StringBuilder(text);
		for(int i = 0; i < builder.length(); i++) {
			Character key = contains(builder.charAt(i));
			if(key != null)
				builder.setCharAt(i, key);
		}
		return builder.toString();
	}
	
	private static Character contains(Character ch) {
		for(Map.Entry<Character, List<Character>> each : characterMap.entrySet()) {
			if(each.getValue().contains(Character.toLowerCase(ch))) {
				if(Character.isUpperCase(ch))
					return Character.toUpperCase(each.getKey());
				return Character.toLowerCase(each.getKey());
			}
		}
		return null;
	}
}